package com.amigo.online.provider.ffmpeg.util;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.nio.file.Files;

@Component
@Data
@Slf4j
public class UploadTempUtil {
	@Value("${upload.url}")
	private String url;

	@Value("${upload.token}")
	String token;
//	public  String uploadResource(MultipartFile[] files, String token)
//			throws ClientProtocolException, IOException {
//		CloseableHttpClient client = HttpClients.createDefault();
//
//		HttpPost upload = new HttpPost(url);
//		upload.addHeader("ACCESS_TOKEN", token);
//		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//		for (MultipartFile multipartFile : files) {
//			builder.addBinaryBody("file", multipartFile.getBytes(), ContentType.APPLICATION_OCTET_STREAM,
//					multipartFile.getOriginalFilename());
//		}
//		HttpEntity entity = builder.build();
//		upload.setEntity(entity);
//		CloseableHttpResponse response = client.execute(upload);
//		HttpEntity entity2 = response.getEntity();
//		String result = EntityUtils.toString(entity2, "UTF-8");
//		System.out.println(result);
////		Response parseObject = new Gson().fromJson(result, Response.class);
//		return result;
//	}

	public String uploadResourceVideo(MultipartFile file,String token)
			throws ClientProtocolException, IOException {
		return upload(file, token);
	}


	/**
	 * 使用配置的token
	 * @param file
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public MultipartFile fileTransformationMultipartFile(File videoFile){
		MultipartFile mulFile = null;
//            String waterMarkUrl = FastDFSClient.uploadFile(videoFile);
		try{
			FileItem fileItem = new DiskFileItem("mainFile", Files.probeContentType(videoFile.toPath()), false, videoFile.getName(), (int) videoFile.length(), videoFile.getParentFile());
			try (InputStream input = new FileInputStream(videoFile); OutputStream os = fileItem.getOutputStream();) {
				IOUtils.copy(input, os);
				mulFile = new CommonsMultipartFile(fileItem);
			}catch (Exception e){
				log.error("文件转换失败",e);
			}
		}catch (Exception e){
			log.error("文件转换失败",e);
		}
		return mulFile;
	}
	/**
	 * 使用配置的token
	 * @param file
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public String uploadResourceVideo(MultipartFile file) throws IOException {
		return upload(file, token);
	}
	public String uploadResourceImg(MultipartFile file, String token)
			throws  IOException {
		CloseableHttpClient client = HttpClients.createDefault();
		return upload(file,token);
	}
	private String upload(MultipartFile file, String token) throws IOException {
		try{
			CloseableHttpClient client = HttpClients.createDefault();
			HttpPost upload = new HttpPost(url);
			upload.addHeader("ACCESS_TOKEN", token);
			MultipartEntityBuilder builder = MultipartEntityBuilder.create();
			builder.addBinaryBody("file", file.getBytes(), ContentType.APPLICATION_OCTET_STREAM,
					file.getOriginalFilename());
			HttpEntity entity = builder.build();
			upload.setEntity(entity);
			CloseableHttpResponse response = client.execute(upload);
			HttpEntity entity2 = response.getEntity();
			String result = EntityUtils.toString(entity2, "UTF-8");
			System.out.println(result);
			JSONObject jsonObject = JSONObject.parseObject(result);
			result = jsonObject.getJSONArray("data").getString(0);
			return result;

		}catch (Exception e){
			log.info("文件上传失败", e);
			throw e;
		}

	}
	public static void main(String[] args) throws IOException {
		UploadTempUtil uploadTempUtil = new UploadTempUtil();

		uploadTempUtil.setUrl("http://128.14.157.130:80/upload");
		File file = new File("D:/amigo/ffmpeg/video/14275220190627112601406.mp4");
		FileInputStream fileInputStream = new FileInputStream(file);

		MultipartFile mulFile = new MockMultipartFile(file.getName(), file.getName(),
				ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
		String waterMarkUrl = uploadTempUtil.uploadResourceVideo(mulFile,"tevitalk");
		System.out.println(waterMarkUrl);
	}
}
